﻿using AnQinManagermentBackendAPI.Common.Request;

namespace AnQinManagermentBackendAPI.Service
{
    public class UpLoadService
    {
        /**
         * 附件上传
         */
        public async Task<RequestResponseModel<string>> FileUpload(IFormFile file)
        {
            //判断文件是否有效
            if (file == null)
            {
                return RequestResponse.CreateResponse(400, "文件为空", "");
            }

            /**
             * 获取时间
             */
            DateTime dateTime = DateTime.Now;

            //获取文件名并存储
            var fileName = Path.GetFileName(file.FileName);
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Uploads", fileName);

            //确保上传目录存在
            Directory.CreateDirectory(Path.GetDirectoryName(filePath));

            //保存文件
            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            //返回
            return RequestResponse.CreateResponse(200, "上传成功", fileName);
        }
    }
}
